package _08作业.t4;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class UploadServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        ExecutorService pool = Executors.newFixedThreadPool(10);
        while (true){
            Socket socket = serverSocket.accept();

           pool.submit(new Runnable() {
               @Override
               public void run() {
                   try {
                       InputStream sis = socket.getInputStream();
                       UUID fileName = UUID.randomUUID();
                       FileOutputStream fileOutputStream = new FileOutputStream("day12\\服务端文件夹\\"+fileName+".jpg");

                       byte[] bytes = new byte[1024];
                       int read;
                       while ((read=sis.read(bytes))!=-1){
                           fileOutputStream.write(bytes,0,read);
                       }
                       System.out.println("服务端下载完成");

                       socket.getOutputStream().write("服务端已下载文件".getBytes());
                       fileOutputStream.close();
                       sis.close();
                       socket.close();
                   } catch (FileNotFoundException e) {
                       e.printStackTrace();
                   } catch (IOException e) {
                       e.printStackTrace();
                   }
               }
           });



        }
    }
}
